package dao.impl;

import dao.ExamTypeDao;
import entity.ExamType;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import utils.DbUtil;

import java.util.List;

/**
 * Created by honghao on 2017/2/9.
 */
public class ExamTypeDaoImpl implements ExamTypeDao {
    private static SessionFactory sf = DbUtil.getSf();

    @Override
    public ExamType get(int id) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        ExamType et = (ExamType) session.get(ExamType.class, id);
        tx.commit();
        return et;
    }

    @Override
    public void save(ExamType examType) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.save(examType);
        tx.commit();
    }

    @Override
    public void delete(ExamType examType) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.delete(examType);
        tx.commit();
    }

    @Override
    public void delete(int id) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        ExamType et = (ExamType) session.get(ExamType.class, id);

        session.delete(et);
        tx.commit();
    }

    @Override
    public void update(ExamType examType) {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        session.update(examType);
        tx.commit();
    }

    @Override
    public List<ExamType> findAll() {
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();

        Query query = session.createQuery("from ExamType ");
        return query.list();
    }
}
